package com.shane.scrollbar
{
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	import flash.display.MovieClip;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	
	public class ShanesScrollBar extends Sprite
	{
		private var thumbMin:int = 0;
		private var thumbMax:int = 210;
		private var windowHieght:int;
		private var yOffset:Object = new Object();
		private var thumbOffest:Number = 0;
	//	public var track:myScrollbarTrack = new myScrollbarTrack();
	//	public var thumb:myScrollbarThumb = new myScrollbarThumb();
		public var trackHeight:Number = 0;
		private var thumbTop:int = 0;
		private var ratio:Number = 0;
		private var listHeight:int = 0;
		private var dummyObj:Object = new Object();
		private var easeDuration:Number;
		private var bottomPadding:Number;
		private var _thumb:MovieClip;
		private var _track:MovieClip;
		private var _contents:MovieClip;
		
		public function ShanesScrollBar() {
			//initScrollBar(212, .3, 30);
		}
		public function initScrollBar($windowHeight:Number, $duration:Number = 1, $bottomPadding:Number = 0) :void {
			this._thumb = parent.getChildByName("thumb") as MovieClip;
			this._track = parent.getChildByName("track") as MovieClip;
			this._contents  = parent.getChildByName("contents") as MovieClip;
			easeDuration = $duration;
			bottomPadding = $bottomPadding;
			//track.name = "track";
			//addChild(track);
			//thumb.name = "thumb";
			_thumb.y = thumbTop;
			//addChild(thumb);
			listHeight = _contents.height;
			windowHieght = _track.height;
			_thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown, false, 0, true);
			_thumb.buttonMode = true;
			_thumb.useHandCursor = true;
			this.yOffset.tar =0;
			this.yOffset.actual =0;
			_track.addEventListener(MouseEvent.MOUSE_DOWN, trackDown, false, 0, true);
			this.addEventListener(MouseEvent.MOUSE_WHEEL,mouseWheelHandler);
			changeHeight($windowHeight);
		}
		private function mouseWheelHandler (e:MouseEvent) :void {
			_thumb.y-=(e.delta*2);
			if (_thumb.y < this.thumbMin) {
				_thumb.y = this.thumbMin;
			} else if (_thumb.y > this.thumbMax) {
				_thumb.y = this.thumbMax;
			}
			ratio = _thumb.y / (this.thumbMax - this.thumbMin);
			yOffset.tar = (- listHeight)*ratio;
			TweenLite.to(yOffset, easeDuration,{delay:0, ease:Quint.easeOut, onUpdate:manageVisible, actual:yOffset.tar});
		}
		public function changeHeight($windowHieght:int) :void {
			windowHieght = $windowHieght;
			thumbSize();
			thumbMax = windowHieght - _thumb.height;
			//thumb.y = this.thumbMin;
			_thumb.y = this.ratio * (thumbMax - thumbMin);
			
			yOffset.tar = (- listHeight)*ratio;
			TweenLite.to(yOffset, easeDuration,{delay:0, ease:Quint.easeOut, onUpdate:manageVisible, actual:yOffset.tar});
			findListHeight();
			
			manageVisible();
		}
		private function thumbSize() :void {
			_thumb.mid.height = windowHieght - (windowHieght / (windowHieght/this.listHeight) - (_thumb.bot.height*2)+15);
			_thumb.bot.y = _thumb.mid.height + _thumb.mid.y;
			thumbMax = windowHieght - _thumb.height;
			if(_thumb.height < this.windowHieght) {
				_thumb.visible = true;
			} else {
				_thumb.visible = false;
			}
		}
		private function trackDown (e:MouseEvent) :void {
			stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp, false, 0, true);
			this.addEventListener(Event.ENTER_FRAME, thumbMove, false, 0, true);
			thumbOffest = _thumb.height*.5;
		}
		private function thumbDown (e:MouseEvent) :void {
			stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp, false, 0, true);
			this.addEventListener(Event.ENTER_FRAME, thumbMove, false, 0, true);
			thumbOffest = e.target.parent.mouseY;
		}
		private function thumbUp (e:MouseEvent) :void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, thumbUp, false);
			this.removeEventListener(Event.ENTER_FRAME, thumbMove, false);
			//yOffset.tar = Math.round(((- listHeight)*ratio)/roundTo) * roundTo;
			TweenLite.to(yOffset, easeDuration*2,{delay:0, onUpdate:manageVisible, actual:yOffset.tar});
		}
		private function thumbMove (e:Event = null) :void {
			_thumb.y = mouseY - this.thumbOffest;
			if (_thumb.y < this.thumbMin) {
				_thumb.y = this.thumbMin;
			} else if (_thumb.y > this.thumbMax) {
				_thumb.y = this.thumbMax;
			}
			ratio = _thumb.y / (this.thumbMax - this.thumbMin);
			yOffset.tar = (- listHeight)*ratio;
			TweenLite.to(yOffset, easeDuration,{delay:0, ease:Quint.easeOut, onUpdate:manageVisible, actual:yOffset.tar});
			
		}
		private function manageVisible() :void {
			_contents.y = yOffset.actual ;
		}
		public function _trackHeight($h:Number):void {
			trackHeight = $h;
			_track.height = $h;
		}
		
		private function findListHeight():void {
			listHeight = _contents.height - windowHieght + bottomPadding;
			trace(listHeight+"listHeight");
		}
		
	
	}
}